<?php
namespace app\members\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\index\common;
use app\index\controller\Log\InsertLog as IL;
class Clue extends Controller
{
    public function __construct() {
        parent::_initialize();
       
      }

    public function is_isset($value='',$date='')
    {
      $list=explode(',',$value);
      foreach ($list as $key => $v) {
        $data["$v"]=isset($date["$v"])?$date["$v"]:'0';
      }
      return $data;
    }
    /**
     * 刘家腾
     * 2018-5-22
     * 添加线索
     */
    public function add_cluelists()
    {

    }
    /**
     * 刘家腾
     * 2018-5-22
     * 获取线索列表
     */
    public function get_cluelists()
    {
        $data['code']=666;
        $data['msg']="操作成功！";
        $single_page=20;//一页多少条
        $user_info=Session::get("user");
        $post=input('post.');
        $map=array();
        $map['c.system_id']=$user_info['system_id'];
        $map['c.business_id']=$this->business_id;
        $map['cl.state']=0;
        $map['cl.is_leadcustomers']=1;//只看线索记录
        $issetdate = 'store_id,sales_id,distribution,is_followup,is_toshop,is_deal,produce_time,distribution_time,followup_time,brandname_phone,page,is_distribution';
        $datas=self::is_isset($issetdate,$post);

        if($user_info['shop']==1){//登录账号是店铺 只能查看当前店铺产生的线索
          $map['c.store_id']=$user_info['id'];
        }elseif($user_info['is_business']=='1'){//店铺ID
          if($datas['store_id']!=0){
            $map['c.store_id']=$datas['store_id'];
          }
          
        }

        if($datas['sales_id']!='0'){//分配销售id
          $map['cl.sales_id']=$datas['sales_id'];
        }
        if($datas['is_distribution']!='0'){//是否分配
          $map['cl.is_distribution']=$datas['is_distribution'];
        }
        if($datas['is_followup']!='0'){//是否跟进
          $map['cl.is_followup']=$datas['is_followup'];
        }
        if($datas['is_toshop']!='0'){//是否到店
          $map['cl.is_toshop']=$datas['is_toshop'];
        }


        $page=$datas['page'];
        if($page>=1){
          $page=$page-1;
        }

        if($datas['brandname_phone']!='0'){//品牌名/手机号
          // $mapor['u.phone']=$datas['brandname_phone'];
          // $mapor['s.store_phone']=$datas['brandname_phone'];

          $date_count= Db::table('customer_log')
                          ->alias("cl")
                          ->join("customer c","cl.user_id=c.user_id","left")
                          ->join("users u","cl.user_id=u.id","left")
                          ->join("business b","cl.business_id=b.business_id","left")
                          ->join("store s","cl.store_id=s.user_id","left")
                          ->join("staff st","cl.assignee_id=st.user_id","left")
                          ->join("staff stt","cl.sales_id=stt.user_id","left")
                          ->where(function ($query) use($map,$datas,$page,$single_page) {
                            
                              $query->where($map);

                              if(! empty($datas['produce_time'])){
                                $query->where("cl.produce_time",">=" ,$datas['produce_time']);#线索产生时间
                              }

                              if(! empty($datas['distribution_time'])){
                                $query->where("cl.distribution_time",">=" ,$datas['distribution_time']);#线索分配时间
                              }

                              if(! empty($datas['followup_time'])){
                                $query->where("cl.followup_time",">=" ,$datas['followup_time']);#线索跟进时间
                              }
                              if($datas['is_deal']==1){#是否成交
                                $query->where("cl.batchcode" ,0);
                              }elseif($datas['is_deal']==2){
                                $query->where("cl.batchcode" ,">",0);
                              }

                          })->where(function ($query) use($datas) {
                              $query->where('s.store_name',$datas['brandname_phone'])

                                   ->whereOr('cl.brand_demand', 'like', "%".$datas['brandname_phone']."%");
                          })->count();

          $data['data'] = Db::table('customer_log')
                          ->alias("cl")
                          ->join("customer c","cl.user_id=c.user_id","left")
                          ->join("users u","cl.user_id=u.id","left")
                          ->join("business b","cl.business_id=b.business_id","left")
                          ->join("store s","cl.store_id=s.user_id","left")
                          ->join("staff st","cl.assignee_id=st.user_id","left")
                          ->join("staff stt","cl.sales_id=stt.user_id","left")
                          ->where(function ($query) use($map,$datas,$page,$single_page) {

                              $query->where($map);

                              if(! empty($datas['produce_time'])){
                                $query->where("cl.produce_time",">=" ,$datas['produce_time']);#线索产生时间
                              }

                              if(! empty($datas['distribution_time'])){
                                $query->where("cl.distribution_time",">=" ,$datas['distribution_time']);#线索分配时间
                              }

                              if(! empty($datas['followup_time'])){
                                $query->where("cl.followup_time",">=" ,$datas['followup_time']);#线索跟进时间
                              }
                              if($datas['is_deal']==1){#是否成交
                                $query->where("cl.batchcode" ,0);
                              }elseif($datas['is_deal']==2){
                                $query->where("cl.batchcode" ,">",0);
                              }
                          })->where(function ($query) use($datas) {
                              $query->where('s.store_name',$datas['brandname_phone'])
                                   ->whereOr('cl.brand_demand', 'like', "%".$datas['brandname_phone']."%");
                          })->field("cl.user_id,cl.source,cl.follow_content,cl.is_toshop,cl.sales_id,stt.name sales_name,stt.phone sales_phone,cl.batchcode,cl.createtime,cl.produce_time,cl.distribution_time,cl.followup_time,cl.assignee_id,st.name assignee_name,cl.is_followup,cl.is_distribution")
                          ->order('cl.id desc')
                          ->limit($page*$single_page,$single_page)
                          ->select();
        }else{
          $date_count = Db::table('customer_log')
                          ->alias("cl")
                          ->join("customer c","cl.user_id=c.user_id","left")
                          ->join("users u","cl.user_id=u.id","left")
                          ->join("business b","cl.business_id=b.business_id","left")
                          ->join("store s","cl.store_id=s.user_id","left")
                          ->join("staff st","cl.assignee_id=st.user_id","left")
                          ->join("staff stt","cl.sales_id=stt.user_id","left")
                          ->where(function ($query) use($map,$datas,$page,$single_page) {
                            
                              $query->where($map);

                              if(! empty($datas['produce_time'])){
                                $query->where("cl.produce_time",">=" ,$datas['produce_time']);#线索产生时间
                              }

                              if(! empty($datas['distribution_time'])){
                                $query->where("cl.distribution_time",">=" ,$datas['distribution_time']);#线索分配时间
                              }

                              if(! empty($datas['followup_time'])){
                                $query->where("cl.followup_time",">=" ,$datas['followup_time']);#线索跟进时间
                              }
                              if($datas['is_deal']==1){#是否成交
                                $query->where("cl.batchcode" ,0);
                              }elseif($datas['is_deal']==2){
                                $query->where("cl.batchcode" ,">",0);
                              }
                          })
                         ->count();

          $data['data'] = Db::table('customer_log')
                          ->alias("cl")
                          ->join("customer c","cl.user_id=c.user_id","left")
                          ->join("users u","cl.user_id=u.id","left")
                          ->join("business b","cl.business_id=b.business_id","left")
                          ->join("store s","cl.store_id=s.user_id","left")
                          ->join("staff st","cl.assignee_id=st.user_id","left")
                          ->join("staff stt","cl.sales_id=stt.user_id","left")
                         ->where(function ($query) use($map,$datas,$page,$single_page) {
                            
                              $query->where($map);

                              if(! empty($datas['produce_time'])){
                                $query->where("cl.produce_time",">=" ,$datas['produce_time']);#线索产生时间
                              }

                              if(! empty($datas['distribution_time'])){
                                $query->where("cl.distribution_time",">=" ,$datas['distribution_time']);#线索分配时间
                              }

                              if(! empty($datas['followup_time'])){
                                $query->where("cl.followup_time",">=" ,$datas['followup_time']);#线索跟进时间
                              }
                              if($datas['is_deal']==1){#是否成交
                                $query->where("cl.batchcode" ,0);
                              }elseif($datas['is_deal']==2){
                                $query->where("cl.batchcode" ,">",0);
                              }
                          })
                         ->field("cl.user_id,cl.source,cl.follow_content,cl.is_toshop,cl.sales_id,stt.name sales_name,stt.phone sales_phone,cl.batchcode,cl.createtime,cl.produce_time,cl.distribution_time,cl.followup_time,cl.assignee_id,st.name assignee_name,cl.is_followup,cl.is_distribution")
                         ->order('cl.id desc')
                         ->limit($page*$single_page,$single_page)
                         ->select();
        }
//$data['sql'] =Db::table('customer_log')->getLastSql();
          $pages=ceil($date_count/$single_page);
          $data['pages']=$pages;//总页数
          $data['date_count']=$date_count;//总条数
          $data['page']=$page+1; //当前页
          if($data['page'] > $pages){
            $data['code']=30003;
            $data['msg']="没有那么多页面！";
          }
          if ($data['page']<=1 && $pages==0) {
            $data['code']=666;
            $data['msg']="没有查询到相关数据！";
          }
          $data['single_page']=$single_page;//每页数量
        return $data;
    }
}